package view.protocol;




import java.rmi.RemoteException;

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

import server.FirewallException;
import server.Protocol;
import view.ErrorFrame;
import view.RuleTable;
import view.RulesAdvancedTable;
import view.Settings;

import controller.FirewallSession;

/**
 * Table for protocols view.
 */
public class ProtocolTable extends JTable
{	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1839439430303029845L;
	/* Table data */
	public Protocol[] protocolData;
	protected final static String columnNames[] ={"Protocol", "IN RegExp", "OUT RegExp"};
	/*controlled object */
	public FirewallSession UI;
	
	public ProtocolTable(int height, FirewallSession UI)
	{
		super(height, columnNames.length);
	    setAutoResizeMode (JTable.AUTO_RESIZE_OFF);
	    autoResizeMode =1 ;
		this.UI = UI;
	}

	
	/**
	 * Createa and returns table header.
	 * @return the header of the table
	 */
	public JTableHeader getProtocolTableHeader()
	{
		String[] columnNames = getColumnsNames();
		
		TableColumn column = null;
		for (int i = 0; i < columnNames.length; i++)
		{
		    column = getColumnModel().getColumn(i);
		    column.setHeaderValue(columnNames[i]);
		}
		
		return getTableHeader();	
	}
	

	/**
	 * Returns the table columns names
	 * @return - the table columns names
	 */
	protected String[] getColumnsNames() 
	{
		return columnNames;
	}
	
	/**
	 * Take the data from UI.
	 */
	private void getProtocolData() 
	{
		/* Current FW ruleset */
		try {
				
			protocolData = UI.getProtocols();
		} catch (RemoteException e) {
			ErrorFrame.AddError("Can't update rules tables :" + e.getMessage(), new JFrame());
		} catch (FirewallException e) {
			ErrorFrame.AddError("Can't update rules tables :" + e.getMessage(), new JFrame());
		}
	}

	/**
	 * Fill the table.
	 */
	public void build()
	{
		getProtocolData();
		
		if(protocolData != null || protocolData.length == 0)
		{
			try{
				/* Add new rows if it's needed */
				int diff = protocolData.length - getModel().getRowCount();
				
				/* Clear the previous data */
				((DefaultTableModel)getModel()).setRowCount(0);
				if(diff > 0)
				{
					((DefaultTableModel)getModel()).setRowCount(protocolData.length);
				}
				else
				{
					((DefaultTableModel)getModel()).setRowCount(Settings.DEFAULT_RULES_WINDOW_DEPTH);
				}
				
				/* Fill the data */
				for(int i = 0; i < protocolData.length; i++)
				{
						if ( protocolData[i] == null){
							throw new FirewallException("Bad data got from server: null pointer on index" + i );
						}
					
					int j = 0;
					this.setValueAt(protocolData[i].protocol, i, j++);
					this.setValueAt(protocolData[i].inReg,    i, j++);
					this.setValueAt(protocolData[i].outReg,   i, j++);
				}
		
			}catch (FirewallException e){
				ErrorFrame.AddError("Failed to update rule talble: " + e.getMessage(), new JFrame());
			}
		}
		else /*if(loggerData != null)*/
		{
			/* Clear the previous data */			
			((DefaultTableModel)getModel()).setRowCount(0);
			((DefaultTableModel)getModel()).setRowCount(Settings.DEFAULT_PROTOCOL_WINDOW_DEPTH);
		}
	}

	
}
